import preprocess.NewFeatureProcess as NFP
import operator
import numpy as np
import pandas as pd
from sklearn import svm
from sklearn.metrics import accuracy_score
from sklearn.preprocessing import LabelEncoder

def refomat(X_features,Y):
    train_feature = X_features['train']
    train_category = Y['train']
    test_feature = X_features['test']
    test_category = Y['test']
    
    all_category = np.unique(np.hstack((train_category,test_category)))
        
    le = LabelEncoder()
    le.fit(all_category)
    
    train_category = le.transform(train_category)
    test_category = le.transform(test_category)
    
    return train_feature,test_feature,train_category,test_category,le

def set_params(Y_train,Y_test):
    label_size = len(np.unique(np.hstack((Y_train,Y_test))))
    params = {}
    params['boosting_type'] = 'dart'
    params['objective'] = 'multiclass'
    params['metric'] = 'multi_error'
    params['num_class'] = label_size
    params['max_bin'] = 255
    params['learning_rate'] = 0.01
    params['num_leaves'] = 2**6 - 1
    params['min_data_in_leaf'] = 2 # important parameter 
    params['min_sum_hessian_in_leaf'] = 0.1
    params['is_unbalance'] = 'true'
    return params

def SVM(X_train,Y_train,X_test,Y_test):
    
    N1 = 3
    N2 = 8
    Title =True
    Website = True
    Char = True
    Add = False
    X = {}
    X['train'] = X_train
    X['test'] = X_test
    Y = {}
    Y['train'] = Y_train
    Y['test'] = Y_test
    X_features = NFP.data_to_feature(X,N1,N2,website=Website,title=Title,char=Char,add=Add,least_feature_appear_websites=2,least_feature_appear_titles=2)
    
    [Train_feature,Test_feature,Train_category,Test_category,le] = refomat(X_features,Y)
    
    
    lin_clf = svm.LinearSVC(random_state=0)
    lin_clf.fit(Train_feature, Train_category) 
    Train_category_prediction = lin_clf.predict(Train_feature)
    print ('SVM : Traning Set Accuracy: %f' % accuracy_score(Train_category,Train_category_prediction))
    Test_category_prediction = lin_clf.predict(Test_feature)
    print ('SVM : Test Set Accuracy: %f' % accuracy_score(Test_category,Test_category_prediction))
    Y_test_prediction = le.inverse_transform(Test_category_prediction)
    
    return Y_test_prediction